package collaborativefiltering4plsusers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetUsersOneWeek {
	
	public static void main (String[] args) throws Exception{
	
		List<Event> eventlist = new ArrayList<Event>();
		List<Event> eventlist1 = new ArrayList<Event>();
		Map<String, List<Event>>map = new HashMap<String, List<Event>>();
		String file = "C:\\Users\\Alket\\junocode\\FlickrAPI\\TuttiIMSIStranieri\\TuttiImsiStranieri_final1883.csv";
		
		loadData(file,eventlist);
		
		for (int i = 0; i < eventlist.size(); i++) {
			
			if(((i+1) - eventlist.size())== 0)
				break;
			String usr = eventlist.get(i).usr;
			String usr1 = eventlist.get(i+1).usr;
			
			if(usr.equals(usr1)){
				
				eventlist1.add(eventlist.get(i));
			}
			else{
				eventlist1.add(eventlist.get(i));
				if(list4map(eventlist1)==1){
					map.put(eventlist.get(i).usr, eventlist1);
					
				}
				eventlist1 = new ArrayList<Event>();
			}
		}
		printMap(map, "IMSIStranieri_di_una_sett2.csv");
		
	}
	
	public static void loadData(String file, List<Event> list) throws Exception{
		
		 BufferedReader br = new BufferedReader(new FileReader(new File(file)));
			
			String line ;
			while((line = br.readLine())!= null){
				String [] riga = line.split(",");
				String usr = riga[0];
				int imsi = Integer.parseInt(riga[1]);
				long celllac = Long.parseLong(riga[2]);
				long ts = Long.parseLong(riga[3]);
				double lat = Double.parseDouble(riga[4]);
				double lon = Double.parseDouble(riga[5]);
				int raggio = Integer.parseInt(riga[6]);
				
				list.add(new Event(usr, imsi, celllac, ts, lat, lon, raggio));
				
	    	}br.close();
		
		
	}
	public static int list4map(List<Event>list){
		long week = 604800000;
		Collections.sort(list);
		Event e = Collections.max(list);
		Event e1 = Collections.min(list);
		long diff = e.ts-e1.ts; 
		//System.out.println("il ts max         = "+e.ts+ " \nmentre quello min = "+e1.ts);
		//System.out.println(diff);
		if((Math.abs(diff))>= week){
			return 1;
		}
		//System.out.println(Math.abs(diff));
		return 0;
		
	}

public static void printMap(Map<String, List<Event>> list, String file) throws Exception{
		
		PrintWriter out = new PrintWriter(new FileWriter(new File(file)));
		for (String s: list.keySet()) {
			for (int j = 0; j < list.get(s).size(); j++) {
				
			out.print(list.get(s).get(j).usr+",");
			out.print(list.get(s).get(j).imsi+",");
			out.print(list.get(s).get(j).cellac+",");
			out.print(list.get(s).get(j).ts+",");
			out.print(list.get(s).get(j).lat+",");
			out.print(list.get(s).get(j).lon+",");
			out.println(list.get(s).get(j).raggio);
			}
		}
		out.close();
	}
}
